﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Material.Interface;
using WMS.FC.Domain.Service.System;
using WMS.FC.Domain.Service.Tray.Interface;

namespace WMS.FC.Domain.Service.Tray
{
    public class AddTrayService : ServiceBase, IAddTrayService
    {
        protected readonly ICheckTrayExistService checkTrayExistService;
        protected readonly IGetTrayBindBarcodeService getTrayBindBarcodeService;
        protected readonly IGetMaterialByBindBarcodeService getMaterialByBindBarcodeService;
        protected readonly ITrayRepository trayRepository;

        public AddTrayService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            ICheckTrayExistService checkTrayExistService,
            IGetTrayBindBarcodeService getTrayBindBarcodeService,
            ITrayRepository trayRepository,
            IGetMaterialByBindBarcodeService getMaterialByBindBarcodeService)
            : base(logger, options, mapper)
        {
            this.checkTrayExistService = checkTrayExistService;
            this.getTrayBindBarcodeService = getTrayBindBarcodeService;
            this.trayRepository = trayRepository;
            this.getMaterialByBindBarcodeService = getMaterialByBindBarcodeService;
        }

        public async Task<ActionResult<TrayPO>> AddTray(string trayBarcode, UserInfo userInfo)
        {
            var result = new ActionResult<TrayPO>("登录托盘");
            try
            {
                var checkTrayExistResult = await checkTrayExistService.CheckTrayExist(trayBarcode);
                if (!checkTrayExistResult.IsSuccess)
                    return result.Fail(checkTrayExistResult);

                if (checkTrayExistResult.ErrorCode == ErrorCode.S)
                    result.Data = checkTrayExistResult.Data;
                else
                {
                    var getTrayBindBarcodeResult = await getTrayBindBarcodeService.GetTrayBindBarcode(trayBarcode);
                    if (!getTrayBindBarcodeResult.IsSuccess)
                        return result.Fail(getTrayBindBarcodeResult);

                    var getTrayMaterialByBindBarcodeResult = await getMaterialByBindBarcodeService.GetTrayMaterialByBindBarcode(getTrayBindBarcodeResult.Data!);
                    if (!getTrayMaterialByBindBarcodeResult.IsSuccess)
                        return result.Fail(getTrayMaterialByBindBarcodeResult);

                    var material = getTrayMaterialByBindBarcodeResult.Data!;
                    var trayPO = new TrayPO()
                    {
                        WarehouseCode = options.Value.WarehouseCode,
                        Barcode = trayBarcode,
                        MaterialCode = material.Code,
                        TechnologyRouteCode = options.Value.EmptyTrayTechnologyRoute,
                        TechnologyProcessCode = options.Value.EmptyTrayTechnologyProcess,
                        ContainerBarcode = trayBarcode,
                        TrayPosition = 1
                    };

                    var count = await trayRepository.Insert(trayPO, userInfo);
                    if (count == 0)
                        return result.DbError("添加托盘数据异常");

                    result.Data = trayPO;
                }
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
